package com.yndsoft.jc.supplier.perfect;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.modelput.Perfect;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class PerfectUpdateActivity extends SupplierBaseActivity implements View.OnClickListener {

    private TextView perfectUpdatePrice;
    private EditText perfectUpdateMoney;
    private EditText perfectUpdateCycle;
    private String pid;
    private String paid;

    @Override
    public int getContentResId() {
        return R.layout.activity_perfect_update;
    }

    public static void start(Context context, String pid, String paid, String price, String money, String time) {
        Intent intent = new Intent(context, PerfectUpdateActivity.class);
        intent.putExtra(Constants.PID, pid);
        intent.putExtra(Constants.PAID, paid);
        intent.putExtra(Constants.PRICE, price);
        intent.putExtra(Constants.MONEY, money);
        intent.putExtra(Constants.TIME, time);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        perfectUpdatePrice = findViewById(R.id.perfect_update_price);
        perfectUpdateMoney = findViewById(R.id.perfect_update_money);
        perfectUpdateCycle = findViewById(R.id.perfect_update_cycle);
        Button perfectUpdateSave = findViewById(R.id.perfect_update_save);

        barTitle.setText("编辑项目");
        barBack.setOnClickListener(this);
        perfectUpdateSave.setOnClickListener(this);
    }

    @Override
    public void initData() {
        pid = getIntent().getStringExtra(Constants.PID);
        paid = getIntent().getStringExtra(Constants.PAID);
        String price = getIntent().getStringExtra(Constants.PRICE);
        String money = getIntent().getStringExtra(Constants.MONEY);
        String time = getIntent().getStringExtra(Constants.TIME);
        perfectUpdatePrice.setText(price != null ? "平台价格: ¥" + price : "平台价格: ¥");
        perfectUpdateMoney.setText(money != null ? money : "");
        perfectUpdateCycle.setText(time != null ? time : "");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.perfect_update_save:
                String money = perfectUpdateMoney.getText().toString().trim();
                String cycle = perfectUpdateCycle.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    showHint("请填写价格！");
                } else {
                    if (TextUtils.isEmpty(cycle)) {
                        showHint("请填写周期！");
                    } else {
                        Perfect perfect = new Perfect();
                        perfect.setSid(mSession.getSid());
                        perfect.setPid(pid);
                        perfect.setPaid(paid);
                        perfect.setPrice(money);
                        perfect.setTime(cycle);
                        put(new Gson().toJson(perfect));
                    }
                }
                break;
        }
    }

    private void put(String json) {
        mHttpUtil.put(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                if (1 == returnMsg.getCode()) {
                    EventModel model = new EventModel();
                    model.setInteger(Constants.PERFECT_UPDATE);
                    EventBus.getDefault().post(model);
                    ToastUtil.show(mContext, "保存成功！");
                    finish();
                } else {
                    ToastUtil.show(mContext, "保存失败！");
                }
            }
        }, "editproduct", json);
    }
}
